package com.jiangjiang.eshop.auth.controller;

import com.jiangjiang.eshop.auth.dto.PriorityDTO;
import com.jiangjiang.eshop.auth.dto.PriorityNode;
import com.jiangjiang.eshop.auth.dto.PrioritySaveDTO;
import com.jiangjiang.eshop.auth.dto.PriorityUpdateDTO;
import com.jiangjiang.eshop.auth.facade.PriorityFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PriorityController
 * @Description TODO
 * @Author jhw
 * @Date 2021/4/20 23:32
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/priority")
public class PriorityController {
    @Autowired
    private PriorityFacade priorityFacade;
    /**
     * 查询所有的权限根目录
     *
     * @return
     */
    @GetMapping("/root")
    public List<PriorityDTO> listRootPriority() {
        try {
            return priorityFacade.listRootPriorities();
        } catch (Exception e) {
            log.warn("PriorityController listRootPriority error", e);
        }
        return new ArrayList<>();
    }
    /**
     * 根据赋权限id查询子权限
     *
     * @param parentId
     * @return
     */
    @GetMapping("/child/{parentId}")
    public List<PriorityDTO> listChildrenPriorities(@PathVariable("parentId") Long parentId) {
        try {
            return priorityFacade.listChildrenPriorities(parentId);
        } catch (Exception e) {
            log.warn("PriorityController listChildrenPriorities error", e);
        }
        return new ArrayList<>();
    }
    /**
     * 添加权限
     *
     * @param priorityDTO 权限对象
     * @return
     */
    @PostMapping("/save")
    public Boolean savePriority(@RequestBody PrioritySaveDTO priorityDTO) {
        try {
            return priorityFacade.save(priorityDTO);
        } catch (Exception e) {
            log.warn("PriorityController savePriority error", e);
        }
        return false;
    }
    /**
     * 更新权限
     *
     * @param priorityUpdateDTO 权限对象
     * @return
     */
    @PostMapping("/modify")
    public Boolean modifyPriority(@RequestBody PriorityUpdateDTO priorityUpdateDTO) {
        try {
            return priorityFacade.update(priorityUpdateDTO);
        } catch (Exception e) {
            log.warn("PriorityController modifyPriority error", e);
        }
        return false;
    }
    /**
     * 根据id查询权限
     *
     * @param id 权限id
     * @return
     */
    @GetMapping("/{id}")
    public PriorityDTO getPriorityById(@PathVariable("id") Long id) {
        try {
            return priorityFacade.queryById(id);
        } catch (Exception e) {
            log.warn("PriorityController getPriorityById error", e);
        }
        return null;
    }
    /**
     * 删除权限
     *
     * @return
     */
    @PostMapping("/{id}")
    public Boolean removePriority(@PathVariable("id") Long id) {
        try {
            return priorityFacade.removePriority(id);
        } catch (Exception e){
            log.warn("PriorityController removePriority error",e);
        }
        return false;
    }

    /**
     * 根据用户账号查询权限树
     * @param accountId 用户账号
     * @return 查询结果
     */
    @GetMapping("/authorized/tree/{accountId}")
    public List<PriorityNode> listAuthorizedTreeByAccountId(@PathVariable("accountId")Long accountId){
        try {
            return priorityFacade.listAuthorizedTreeByAccountId(accountId);
        }catch (Exception e){
            log.warn("PriorityController listAuthorizedTreeByAccountId error",e);
        }
        return null;
    }
}
